﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
/// <summary>
///  的摘要说明
/// </summary>
public class SqlServer
{

		//
		// TODO: 在此处添加构造函数逻辑
		//
    
        //private static string _DBConnString = "server=LAPTOP-O64DGJ13; database=机考系统;uid=sa;pwd=123;";
    //private static string _DBConnString = "server=8.129.19.161,1433;database=机考系统;uid=sa;pwd=Sa19991129;";
    private static string _DBConnString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ToString();
        /// <summary>
        /// 获取连接数据库字符串
        /// </summary>
        public static string DBConnString
        {
            get
            {
                return _DBConnString;
            }
        }
    /// <summary>
    /// 获取数据集
    /// </summary>
    /// <param name="sql"></param>
    /// <returns></returns>
        public static DataSet GetDataSet(string sql)
        {
            if (DBConnString == null || DBConnString == "") return null;
            DataSet dsMyData = new DataSet();
                SqlDataAdapter daAdapter = new SqlDataAdapter(sql, DBConnString);
                daAdapter.Fill(dsMyData);
            return dsMyData;
        }
        /// <summary>
        /// 获取表格datatable
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static DataTable GetDataTable(string sql)
        {
            if (DBConnString == null || DBConnString == "") return null;
            DataSet dsMyData = new DataSet();
            DataTable myDataTable = new DataTable();
            SqlDataAdapter daAdapter = new SqlDataAdapter(sql, DBConnString);
            daAdapter.Fill(dsMyData);
            myDataTable = dsMyData.Tables[0];
            return myDataTable;
        }

        /// <summary>
        /// 非查询语句
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static int ExecuteNonQuery(string sql)
        {
            if (DBConnString == null || DBConnString == "") return 0;
            SqlConnection con = new SqlConnection(DBConnString);
            SqlCommand cmd = new SqlCommand(sql, con);
            con.Open();
            int n = cmd.ExecuteNonQuery();
            con.Close();
            return n;
        }

        /// <summary>
        /// 返回第一行第一列
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static string ExecuteScalar(string sql)
        {
            if (DBConnString == null || DBConnString == "") return null;
            SqlConnection myConn = new SqlConnection(DBConnString);
            SqlCommand myCommand = new SqlCommand(sql, myConn);
            myConn.Open();
            object ret = myCommand.ExecuteScalar();
            myConn.Close();
            if (ret == null) return null;
            return ret.ToString();

        }
    
	
}